package com.nunbey.lingxi.lincat.activityinit;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

/**
 * 所有活动的基类，配合使用了活动集合
 */
public abstract class BaseActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstancedState) {
        super.onCreate(savedInstancedState);
        ActivityCollector.getActivityCollector().addActivity(this);
        showSelfClass();
    }

    @Override
    public void finish(){
        ActivityCollector.getActivityCollector().removeActivity(this);
        //注意此处的finish要在外部调用
        super.finish();
    }

    /**
     * @description 显示当前活动的类
     */
    protected void showSelfClass(){
        Log.d("LinCat/showSelfClass",this.getClass().toString());
    }

    /**
     * @description 获取当前可见的活动类名称
     * @return Class
     */
    protected String getCurrentActivityClassString(){
        ActivityManager am = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);
        // get the info from the currently running task
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        return componentInfo.getClassName();
    }
}
